package edu.pku.sei.transformation.editor.controller.editparts.node;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.EllipseAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.editpolicies.DirectEditPolicy;

import edu.pku.sei.modeler.edit.parts.AbstractNodeEditPart;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.transformation.editor.controller.policies.NodeComponentEditPolicy;
import edu.pku.sei.transformation.editor.controller.policies.TransformationNodeEditPolicy;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.model.test.CheckPoint;
import edu.pku.sei.transformation.editor.views.figures.CheckPointFigure;

public class CheckPointEditpart extends AbstractNodeEditPart {

	public CheckPointEditpart(MetaShapeElement model) {
		super(model);
	}

	@Override
	protected DirectEditPolicy createDirectEditPolicy() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected IFigure createFigure() {
		CheckPoint cp = (CheckPoint) getNode().getModelElement();
		CheckPointFigure figure = new CheckPointFigure();
//		figure.setName(cp.getTypeString());
//		figure.setName(cp.getName());
		return figure;
	}
	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE,
				new TransformationNodeEditPolicy());
		installEditPolicy(EditPolicy.COMPONENT_ROLE,
				new NodeComponentEditPolicy());
	}
	
	@Override
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		CheckPoint cp = (CheckPoint) getNode().getModelElement();
		CheckPointFigure figure = (CheckPointFigure) getFigure();
		String propertyName = event.getPropertyName();
		if (ModelPackage.NAMEDELEMENT_NAME.equals(propertyName)) {
//			figure.setName(cp.getName());
		} else {
			super.handlePropertyChanged(event);
		}
	}
	public ConnectionAnchor getSourceConnectionAnchor(
			ConnectionEditPart connection) {
		if (anchor == null)
			anchor = new EllipseAnchor(((CheckPointFigure)getFigure()).getBody());
		return anchor;
	}

	public ConnectionAnchor getSourceConnectionAnchor(Request request) {
		if (anchor == null)
			anchor = new EllipseAnchor(((CheckPointFigure)getFigure()).getBody());
		return anchor;
	}

	public ConnectionAnchor getTargetConnectionAnchor(
			ConnectionEditPart connection) {
		if (anchor == null)
			anchor = new EllipseAnchor(((CheckPointFigure)getFigure()).getBody());
		return anchor;
	}

	public ConnectionAnchor getTargetConnectionAnchor(Request request) {
		if (anchor == null)
			anchor = new EllipseAnchor(((CheckPointFigure)getFigure()).getBody());
		return anchor;
	}
	
//	@Override
//	protected void refreshVisuals() {
//		super.refreshVisuals();
//		Rectangle constraint = new Rectangle(0, 0, -1, -1);
//		MetaNode node = getNode();
//		Point p = new Point(node.getLocationX(), node.getLocationY());
//		constraint.setLocation(p);
//		constraint.width = 20;
//		constraint.height = 30;
//		((GraphicalEditPart) getParent()).setLayoutConstraint(this,
//				getFigure(), constraint);
//	}
}
